package org.ims.inv.web.action.system;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.ims.inv.model.system.ModelAppRole;
import org.ims.inv.web.action.BaseAppAction;
import org.springframework.beans.BeanUtils;

import cn.trymore.core.exception.ServiceException;
import cn.trymore.core.log.LogAnnotation;
import cn.trymore.core.util.UtilString;
import cn.trymore.core.web.paging.PaginationSupport;
import cn.trymore.core.web.paging.PagingBean;

/**
 * Action: 系统角色管理
 * 
 * @author Jeccy.Zhao
 *
 */
public class SystemRoleAction
extends BaseAppAction
{
	/**
	 * The LOGGER
	 */
	private static final Logger LOGGER = Logger.getLogger(SystemRoleAction.class);
	
	/**
	 * <b>[WebAction]</b> 
	 * <br/>
	 * 权限组配置-列表展现
	 */
	public ActionForward pageRoleIndex (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		try
		{
			ModelAppRole formRole = (ModelAppRole) form;
			
			PagingBean pagingBean = this.getPagingBean(request);
			PaginationSupport<ModelAppRole> roles = this.serviceAppRole.getPagination(formRole, pagingBean);
			
			request.setAttribute("roles", roles);
			request.setAttribute("roleForm", formRole);
			
			// 输出分页信息至客户端
			outWritePagination(request, pagingBean, roles);
			
		} 
		catch (ServiceException e)
		{
			LOGGER.error("Exception raised when fetch all roles.", e);
		}
		
		return mapping.findForward("page.sys.role.index");
	}
	
	/**
	 * <b>[WebAction]</b> 
	 * <br/>
	 * 权限数据弹框页面
	 */
	public ActionForward dialogRolePage (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		try
		{
			String roleId = request.getParameter("roleId");
			if (UtilString.isNotEmpty(roleId))
			{
				if (this.isObjectIdValid(roleId))
				{
					request.setAttribute("entity", this.serviceAppRole.get(roleId));
				}
				else
				{
					return ajaxPrint(response, getErrorCallback("数据ID不合法!"));
				}
			}
		}
		catch (Exception e)
		{
			LOGGER.error("Exception raised when loading role dialog page", e);
			return ajaxPrint(response, getErrorCallback("数据加载失败,原因:" + e.getMessage()));
		}
		
		return mapping.findForward("dialog.sys.role");
	}
	
	/**
	 * <b>[WebAction]</b> 
	 * <br/>
	 * 权限数据保存
	 */
	@LogAnnotation(description="权限新增或更新: `${roleKey}`",type="权限管理")
	public ActionForward actionSavaRole (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		try
		{
			ModelAppRole formEntity = (ModelAppRole)form;
			ModelAppRole entity = formEntity;
			
			boolean isActionEdit = this.isObjectIdValid(formEntity.getId());
			
			if (isActionEdit)
			{
				// 编辑
				entity = this.serviceAppRole.get(formEntity.getId());
				if (entity != null)
				{
					BeanUtils.copyProperties(formEntity, entity, new String[] { "roleStatus" } );
				}
				else
				{
					return ajaxPrint(response, getErrorCallback("权限数据实体不存在!"));
				}
			}
			
			// 保存
			this.serviceAppRole.save(entity);
			
			return ajaxPrint(response, 
					getSuccessCallback("数据保存成功.", CALLBACK_TYPE_CLOSE, CURRENT_NAVTABID, null, false));
		}
		catch (ServiceException e)
		{
			LOGGER.error("Exception raised when saving role data", e);
			return ajaxPrint(response, getErrorCallback("数据加载失败,原因:" + e.getMessage()));
		}
		
	}
	
	/**
	 * <b>[WebAction]</b> 
	 * <br/>
	 * 权限数据移除
	 */
	@LogAnnotation(description="权限删除: `${roleId}`",type="权限管理")
	public ActionForward actionRemoveRole (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		try
		{
			String roleId = request.getParameter("roleId");
			if (this.isObjectIdValid(roleId))
			{
				this.serviceAppRole.remove(roleId);
				
				return ajaxPrint(response, 
					getSuccessCallbackAndReloadCurrent("权限数据删除成功."));
			}
			else
			{
				return ajaxPrint(response, getErrorCallback("数据ID不合法!"));
			}
		}
		catch (ServiceException e)
		{
			LOGGER.error("Exception raised when removing role data", e);
			return ajaxPrint(response, getErrorCallback("数据删除失败,原因:" + e.getMessage()));
		}
		
	}
	
	public static Logger getLogger()
	{
		return LOGGER;
	}
}
